package tclp.gui;

import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import tclp.gui.panels.AnalysisPanel;
import tclp.gui.panels.MainPanel;
import tclp.gui.panels.SettingsScreenPanel;
import tclp.gui.panels.SettingsTrainPanel;

/**
 *
 * @author Absatz Leoš
 */
public class MainFrame extends JFrame implements WindowListener {

    public static final int k_width = 700;
    public static final int k_height = 500;
    private final GUIController guiController;
    
    private SettingsScreenPanel screenPanel;
    private SettingsTrainPanel trainPanel;
    private MainPanel mainPanel;
    private AnalysisPanel analysisPanel;

    public MainFrame(GUIController guiController) {
        this.guiController = guiController;

        configureFrame();
        addComponents();
        addWindowListener(this);
    }

    private void configureFrame() {
        this.setSize(k_width, k_height);
        this.setMinimumSize(new Dimension(k_width, k_height));
        this.setTitle("TERA Combat Log Parser");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void addComponents() {
        JTabbedPane tabbedPane = new JTabbedPane();

        mainPanel = new MainPanel(guiController);
        analysisPanel = new AnalysisPanel(guiController);
        trainPanel = new SettingsTrainPanel(guiController);
        screenPanel = new SettingsScreenPanel(guiController);
        tabbedPane.addTab("Main", mainPanel);
        tabbedPane.addTab("Combat Log Analysis", analysisPanel);
        tabbedPane.addTab("Screen Alignment", screenPanel);
        tabbedPane.addTab("Training Images", trainPanel);
        
        this.add(tabbedPane);
    }
    
    public MainPanel getMainPanel() {
        return mainPanel;
    }

    public void windowOpened(WindowEvent e) {
        try {
            //Initialize native hook.  This is done on window open because the
            //listener requires the keyListener object to be constructed.
            //Note: This function is not AWT Thread Safe!
            GlobalScreen.getInstance().registerNativeHook();
            GlobalScreen.getInstance().addNativeKeyListener(guiController.getKeyListener());
        } catch (NativeHookException ex) {
            System.out.println(ex);
            System.runFinalization();
            System.exit(1);
        }
    }

    public void windowClosing(WindowEvent e) {
        try {
            //Note: This function is not AWT Thread Safe!
            GlobalScreen.getInstance().unregisterNativeHook();
            GlobalScreen.getInstance().removeNativeKeyListener(guiController.getKeyListener());
        } catch (NativeHookException ex) {
            System.out.println(ex);
            System.runFinalization();
            System.exit(1);
        }
    }

    public void windowClosed(WindowEvent e) {
        // noop
    }

    public void windowIconified(WindowEvent e) {
        // noop
    }

    public void windowDeiconified(WindowEvent e) {
        // noop
    }

    public void windowActivated(WindowEvent e) {
        // noop
    }

    public void windowDeactivated(WindowEvent e) {
        // noop
    }
}
